package org.dev.vlp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dev.vlp.entity.ComUser;
import org.dev.vlp.entity.Community;
import org.dev.vlp.exception.ApiException;
import org.dev.vlp.service.ComUserService;
import org.dev.vlp.mapper.ComUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86184
* @description 针对表【vlp_com_user】的数据库操作Service实现
* @createDate 2023-01-08 19:04:44
*/
@Service
public class ComUserServiceImpl extends ServiceImpl<ComUserMapper, ComUser>
    implements ComUserService{
    @Autowired
    private ComUserMapper comUserMapper;

    @Override
    public void joinCommunity(int comId,int userId) {
        QueryWrapper<ComUser> comUserQueryWrapper = new QueryWrapper<>();
        comUserQueryWrapper.eq("com_id",comId).eq("user_id",userId);
        ComUser comUser1 = comUserMapper.selectOne(comUserQueryWrapper);
        if(comUser1!=null){
            throw new ApiException("已加入，无需重复加入");
        }
        ComUser comUser = new ComUser(comId, userId);
        if(comUserMapper.insert(comUser)<=0){
            throw new ApiException("加入失败");
        }
    }

    @Override
    public List<Community> getMyJoined(int userId) {
        return comUserMapper.getMyJoined(userId);
    }
}




